import * as THREE from 'three';
import * as OIMO from 'oimo';


var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000);

var renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

const world = new OIMO.World({
    timestep: 1/60,
    iterations: 8,
    broadphase: 2,
    worldscale: 1,
    random: true,
    info: false,
});

const groundBody = world.add({
    type: 'box',
    size: [10000, 1, 10000],
    pos: [0, 0, 0],
    rot: [0, 0, 0],
    move: false, // 是否可移动
    density: 1, // 密度
    friction: 1, // 摩擦力
    restitution: 0.3, // 弹性
    belongsTo: 1,
});

const body = world.add({
    type: 'sphere',
    size: [3], // 半径
    pos: [0, 10, 0], // 位置
    rot: [0, 0, 0], // 旋转
    move: true, // 是否可移动
    density: 10, // 密度
    friction: 0.2, // 摩擦力
    restitution: 2, // 弹性
    belongsTo: 1,
    collidesWith: 0xffffffff, // 0xffffffff
});


var geometry = new THREE.SphereGeometry(5, 10, 10);
var sphereMaterial = new THREE.MeshStandardMaterial({ color: 0x00ff00 });
var sphere = new THREE.Mesh(geometry, sphereMaterial);
sphere.position.copy(body.getPosition());
scene.add(sphere);

var planeGeometry = new THREE.PlaneGeometry(10000, 10000, 1, 1);
var planeMaterial = new THREE.MeshStandardMaterial({
    color: 0x00ff00,
    roughness: 0.5,
    metalness: 0.5
});


/*var plane = new THREE.Mesh(planeGeometry, planeMaterial);
plane.position.copy(groundBody.getPosition())
scene.add(plane);*/

const cube = new THREE.Mesh(
    new THREE.BoxGeometry(1, 1, 1),
    new THREE.MeshBasicMaterial({
        color: 0x607d8b,
    }),
)
scene.add(cube)
function animate() {
    requestAnimationFrame( animate );
    world.step();
    sphere.position.copy(body.getPosition());
    sphere.quaternion.copy(body.getQuaternion());
    console.log(body.getPosition())
    renderer.render(scene, camera);

}
animate();